package concurrent.cas;

import org.junit.Test;

import java.util.concurrent.atomic.AtomicReference;

/**
 * 原子引用
 */
public class CasAtomicReference {

	@Test
	public void test() {
		User user1 = new User("tom", 12);
		User user2 = new User("eve", 11);

		AtomicReference<User> reference = new AtomicReference<>();
		reference.set(user1);

		System.out.println("第一次尝试交换 - " + reference.compareAndSet(user1, user2) + " - reference=" + reference.get().toString());
		System.out.println("第一次尝试交换 - " + reference.compareAndSet(user1, user2) + " - reference=" + reference.get().toString());
	}

}

class User {

	String userName;

	int age;

	public User(String userName, int age) {
		this.userName = userName;
		this.age = age;
	}

	@Override
	public String toString() {
		return String.format("User [userName=%s, age=%s]", userName, age);
	}

}
